using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using WorkflowProcesses = Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using WorkflowRelevantData = Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;
using WorkflowParticipantsData = Scenario.WorkflowEngine.DataModel.WorkflowParticipantsData;
using WorkflowDataTypes = Scenario.WorkflowEngine.DataModel.WorkflowDataTypes;

using WAPI = Scenario.WAPI;
using Scenario.Modeler;
using Scenario.WorkflowEngine;

namespace Scenario.Forms.Workflow
{
	/// <summary>
	/// Descrizione di riepilogo per ProcessProperties.
	/// </summary>
	public class ProcessInstanceManager : System.Windows.Forms.Form
	{
        public WorkflowProcesses.XMLWorkflow xmlWorkflow = null;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Button btnSave;
        private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox cmbValues;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.ComboBox cmbUsers;
        private System.Windows.Forms.ListView lstActivities;
		private System.Windows.Forms.ListView lstAttributes;
		private System.Windows.Forms.ListView lstActivityAttribute;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;
        private Label label1;
        private TextBox txtName;
        private TextBox txtProcessId;
        private Label label5;
        private Label label6;
        private TextBox txtDescription;
        private Label label7;
        private TextBox txtStatus;

		private ArrayList vAttributesActivityModified = new ArrayList();

        public ProcessInstanceManager() 
        {
            //
            // Necessario per il supporto di Progettazione Windows Form
            //
            InitializeComponent();
        }

        public void LoadProcess(String processID)
        {
            WorkflowProcesses.XMLWorkflow workflow = Engine.ProcessHelper.GetWorkflowInstance(processID);
            this.LoadProcess(ref workflow);
        }

        public void LoadProcess (ref WorkflowProcesses.XMLWorkflow workflow )
        {
            xmlWorkflow = workflow;
            this.buidForm();
        }

		private void buidForm()
		{
			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//
			if ( xmlWorkflow != null )
			{
                this.txtName.Text = xmlWorkflow.Name;
                this.txtDescription.Text = xmlWorkflow.Description;
                //this.txtParentProcess.Text = xmlWorkflow.ParentProcess;
                this.txtStatus.Text = xmlWorkflow.Status;

                try
				{
                    WorkflowRelevantData.XMLAttribute attr = xmlWorkflow.XMLAttributes.Get("WorkflowProcess.Id");
					this.txtProcessId.Text = attr.Value;

					this.MakeGrids(xmlWorkflow);
				}
				catch 
				{
				}
			}

			WorkflowParticipantsData.XMLUsers xmlUsers = Engine.UserHelper.GetUsers();

			foreach ( WorkflowParticipantsData.XMLUser user in xmlUsers)
			{
				this.cmbUsers.Items.Add( user.Key ) ;
			}
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.cmbValues = new System.Windows.Forms.ComboBox();
            this.label4 = new System.Windows.Forms.Label();
            this.cmbUsers = new System.Windows.Forms.ComboBox();
            this.lstActivities = new System.Windows.Forms.ListView();
            this.lstAttributes = new System.Windows.Forms.ListView();
            this.lstActivityAttribute = new System.Windows.Forms.ListView();
            this.label1 = new System.Windows.Forms.Label();
            this.txtName = new System.Windows.Forms.TextBox();
            this.txtProcessId = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.txtDescription = new System.Windows.Forms.TextBox();
            this.label7 = new System.Windows.Forms.Label();
            this.txtStatus = new System.Windows.Forms.TextBox();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(0, 566);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(1065, 49);
            this.panel2.TabIndex = 15;
            // 
            // btnCancel
            // 
            this.btnCancel.Font = new System.Drawing.Font("Garamond", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnCancel.Location = new System.Drawing.Point(520, 7);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(87, 35);
            this.btnCancel.TabIndex = 10;
            this.btnCancel.Text = "CLOSE";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnSave
            // 
            this.btnSave.Font = new System.Drawing.Font("Garamond", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnSave.Location = new System.Drawing.Point(539, 482);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(108, 72);
            this.btnSave.TabIndex = 9;
            this.btnSave.Text = "RESTART Process";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(16, 468);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(84, 20);
            this.label3.TabIndex = 20;
            this.label3.Text = "Result";
            // 
            // cmbValues
            // 
            this.cmbValues.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbValues.Location = new System.Drawing.Point(16, 482);
            this.cmbValues.Name = "cmbValues";
            this.cmbValues.Size = new System.Drawing.Size(487, 21);
            this.cmbValues.Sorted = true;
            this.cmbValues.TabIndex = 21;
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(16, 519);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(84, 20);
            this.label4.TabIndex = 22;
            this.label4.Text = "Users";
            // 
            // cmbUsers
            // 
            this.cmbUsers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbUsers.Location = new System.Drawing.Point(16, 533);
            this.cmbUsers.Name = "cmbUsers";
            this.cmbUsers.Size = new System.Drawing.Size(487, 21);
            this.cmbUsers.Sorted = true;
            this.cmbUsers.TabIndex = 23;
            // 
            // lstActivities
            // 
            this.lstActivities.Location = new System.Drawing.Point(19, 237);
            this.lstActivities.Name = "lstActivities";
            this.lstActivities.Size = new System.Drawing.Size(487, 196);
            this.lstActivities.TabIndex = 24;
            this.lstActivities.UseCompatibleStateImageBehavior = false;
            this.lstActivities.View = System.Windows.Forms.View.Details;
            this.lstActivities.SelectedIndexChanged += new System.EventHandler(this.lstActivities_SelectedIndexChanged);
            this.lstActivities.DoubleClick += new System.EventHandler(this.lstActivities_DoubleClick);
            // 
            // lstAttributes
            // 
            this.lstAttributes.FullRowSelect = true;
            this.lstAttributes.Location = new System.Drawing.Point(551, 23);
            this.lstAttributes.Name = "lstAttributes";
            this.lstAttributes.Size = new System.Drawing.Size(487, 196);
            this.lstAttributes.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.lstAttributes.TabIndex = 25;
            this.lstAttributes.UseCompatibleStateImageBehavior = false;
            this.lstAttributes.View = System.Windows.Forms.View.Details;
            this.lstAttributes.DoubleClick += new System.EventHandler(this.lstAttributes_DoubleClick);
            // 
            // lstActivityAttribute
            // 
            this.lstActivityAttribute.FullRowSelect = true;
            this.lstActivityAttribute.Location = new System.Drawing.Point(551, 237);
            this.lstActivityAttribute.Name = "lstActivityAttribute";
            this.lstActivityAttribute.Size = new System.Drawing.Size(487, 196);
            this.lstActivityAttribute.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.lstActivityAttribute.TabIndex = 26;
            this.lstActivityAttribute.UseCompatibleStateImageBehavior = false;
            this.lstActivityAttribute.View = System.Windows.Forms.View.Details;
            this.lstActivityAttribute.DoubleClick += new System.EventHandler(this.lstActivityAttribute_DoubleClick);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(16, 57);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(51, 13);
            this.label1.TabIndex = 27;
            this.label1.Text = "Processo";
            // 
            // txtName
            // 
            this.txtName.Location = new System.Drawing.Point(83, 57);
            this.txtName.Name = "txtName";
            this.txtName.Size = new System.Drawing.Size(417, 20);
            this.txtName.TabIndex = 28;
            // 
            // txtProcessId
            // 
            this.txtProcessId.Location = new System.Drawing.Point(83, 23);
            this.txtProcessId.Name = "txtProcessId";
            this.txtProcessId.Size = new System.Drawing.Size(100, 20);
            this.txtProcessId.TabIndex = 29;
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(16, 23);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(16, 13);
            this.label5.TabIndex = 30;
            this.label5.Text = "Id";
            this.label5.Click += new System.EventHandler(this.label5_Click);
            // 
            // label6
            // 
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(16, 89);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(62, 13);
            this.label6.TabIndex = 31;
            this.label6.Text = "Descrizione";
            // 
            // txtDescription
            // 
            this.txtDescription.Location = new System.Drawing.Point(85, 89);
            this.txtDescription.Multiline = true;
            this.txtDescription.Name = "txtDescription";
            this.txtDescription.Size = new System.Drawing.Size(415, 76);
            this.txtDescription.TabIndex = 32;
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(16, 183);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(32, 13);
            this.label7.TabIndex = 33;
            this.label7.Text = "Stato";
            // 
            // txtStatus
            // 
            this.txtStatus.Location = new System.Drawing.Point(85, 183);
            this.txtStatus.Name = "txtStatus";
            this.txtStatus.Size = new System.Drawing.Size(100, 20);
            this.txtStatus.TabIndex = 34;
            // 
            // ProcessInstanceManager
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(1065, 615);
            this.Controls.Add(this.txtStatus);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.txtDescription);
            this.Controls.Add(this.label6);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.txtProcessId);
            this.Controls.Add(this.txtName);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.lstActivityAttribute);
            this.Controls.Add(this.lstAttributes);
            this.Controls.Add(this.lstActivities);
            this.Controls.Add(this.cmbUsers);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.cmbValues);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.panel2);
            this.Name = "ProcessInstanceManager";
            this.Text = "ProcessInstance";
            this.Load += new System.EventHandler(this.ProcessInstanceManager_Load);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.xmlWorkflow = null;
			this.Close();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			WorkflowProcesses.XMLActivity Activity = new WorkflowProcesses.XMLActivity();

            if (this.txtProcessId.Text.Equals (String.Empty) ) 
            {
				MessageBox.Show ("It's mandatory a valid id process");
				return;
			}
            if ( this.lstActivities.SelectedItems[0].SubItems[0].Text.Equals (String.Empty) )
            {
                MessageBox.Show ("It's mandatory a valid activity name.");
                return;
            }

            Activity.ProcessId = this.txtProcessId.Text;
            Activity.Name = this.lstActivities.SelectedItems[0].SubItems[0].Text;
			

            Engine.ProcessHelper.SetAttributes(Activity.ProcessId, this.xmlWorkflow.XMLAttributes);

			for ( int i=0; i<this.vAttributesActivityModified.Count;i++)
			{
				string sActivityName = (string)this.vAttributesActivityModified[i];
				WorkflowProcesses.XMLActivity xmlActivity = xmlWorkflow.XMLActivities.Get (sActivityName);
                Engine.ActivityHelper.SetAttributes(xmlActivity.ProcessId, xmlActivity.Name, xmlActivity.XMLAttributes,this.cmbUsers.SelectedItem.ToString());
			}
			
			String sResult = "";
            if (this.cmbValues.SelectedItem != null)
            {
                sResult = this.cmbValues.SelectedItem.ToString();
            }

            try {
                Engine.ActivityHelper.PlayActivity(Activity.ProcessId, 
                                                Activity.Name,
                                                this.cmbUsers.SelectedItem.ToString(),
                                                Scenario.WAPI.ActivityHelper.ActivityOperation.Complete,
                                                sResult);

				MessageBox.Show ("Process restarted correcly");
			}
            catch (Scenario.WAPI.Exception.WAPIException err)
            { 
				MessageBox.Show ("Process restarted not correcly." + err.Message);
			}
		}

		private void cmbActivities_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			
		}

		private void MakeGrids(WorkflowProcesses.XMLWorkflow xmlWorkflow)
		{
			if ( this.lstActivities.Columns.Count == 0 )
			{
				ColumnHeader ch = new ColumnHeader();
				ch.Text = "Name";
				ch.TextAlign = HorizontalAlignment.Right;
				ch.Width = 100;
				this.lstActivities.Columns.Add(ch);

				ch = new ColumnHeader();
				ch.Text = "Status";
				ch.TextAlign = HorizontalAlignment.Left;
				ch.Width = lstActivities.Width - 100;
				this.lstActivities.Columns.Add(ch);
			}
			else
			{
				this.lstActivities.Items.Clear();
			}

			

			if ( this.lstAttributes.Columns.Count == 0 )
			{
				lstAttributes.Columns.Add("Name",100, HorizontalAlignment.Left);
				lstAttributes.Columns.Add ("Value", lstAttributes.Width - 100, HorizontalAlignment.Left);
			}
			else
			{
				this.lstAttributes.Items.Clear();
			}


			foreach ( WorkflowProcesses.XMLActivity activity in xmlWorkflow.XMLActivities)
			{
				

				ListViewItem item = new ListViewItem();
				item.Text = activity.Name;
				item.SubItems.Add(activity.XMLLog.Status);
				this.lstActivities.Items.Add(item);
			}

			foreach ( WorkflowRelevantData.XMLAttribute xmlAttribute in xmlWorkflow.XMLAttributes)
			{
				ListViewItem item = new ListViewItem();
				item.Text = xmlAttribute.Name;
				item.SubItems.Add(xmlAttribute.Value);
				this.lstAttributes.Items.Add(item);
			}
		}

		private void lstAttributes_DoubleClick(object sender, System.EventArgs e)
		{
			if ( lstAttributes.SelectedItems.Count == 0 )
			{
				return;
			}

			WorkflowRelevantData.XMLAttribute xmlAttribute = this.xmlWorkflow.XMLAttributes.Get(this.lstAttributes.SelectedItems[0].SubItems[0].Text);
			FormAttribute form = new FormAttribute(xmlAttribute);
			form.ShowDialog (this);

			if ( form.xmlAttribute != null )
			{
				lstAttributes.SelectedItems[0].SubItems[0].Text = form.xmlAttribute.Name;
				lstAttributes.SelectedItems[0].SubItems[1].Text = form.xmlAttribute.Value;

				xmlAttribute.Name = form.xmlAttribute.Name;
				xmlAttribute.Value = form.xmlAttribute.Value;
			}
		}

		private void lstActivityAttribute_DoubleClick(object sender, System.EventArgs e)
		{
			WorkflowProcesses.XMLActivity xmlActivity = xmlWorkflow.XMLActivities.Get (this.lstActivities.SelectedItems[0].SubItems[0].Text);

			if ( lstAttributes.SelectedItems.Count == 0 )
			{
				return;
			}

			

			WorkflowRelevantData.XMLAttribute xmlAttribute = xmlActivity.XMLAttributes.Get(this.lstActivityAttribute.SelectedItems[0].SubItems[0].Text);
			FormAttribute form = new FormAttribute(xmlAttribute);
			form.ShowDialog (this);

			if ( form.xmlAttribute != null )
			{
				lstActivityAttribute.SelectedItems[0].SubItems[0].Text = form.xmlAttribute.Name;
				lstActivityAttribute.SelectedItems[0].SubItems[1].Text = form.xmlAttribute.Value;

				xmlAttribute.Name = form.xmlAttribute.Name;
				xmlAttribute.Value = form.xmlAttribute.Value;
			}

			vAttributesActivityModified.Add (xmlActivity.Name);
		}

		private void lstActivities_DoubleClick(object sender, System.EventArgs e)
		{
			WorkflowProcesses.XMLActivity activity = xmlWorkflow.XMLActivities.Get (this.lstActivities.SelectedItems[0].SubItems[0].Text);
			if ( this.lstActivityAttribute.Columns.Count == 0 )
			{
				lstActivityAttribute.Columns.Add("Name",100, HorizontalAlignment.Left);
				lstActivityAttribute.Columns.Add ("Value", lstActivityAttribute.Width - 100, HorizontalAlignment.Left);
			}
			else
			{
				this.lstActivityAttribute.Items.Clear();
			}

			foreach ( WorkflowRelevantData.XMLAttribute xmlAttribute in activity.XMLAttributes)
			{
				ListViewItem item = new ListViewItem();
				item.Text = xmlAttribute.Name;
				item.SubItems.Add(xmlAttribute.Value);
				this.lstActivityAttribute.Items.Add(item);
			}
		}

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void ProcessInstanceManager_Load(object sender, EventArgs e)
        {

        }

        private void lstActivities_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

           
	}
}
